import setConfig, { casConfig } from './config';
import check, { checkTicket, checkCookieTicket, checkUrlTicket } from './check';
import setCookie, { cookie } from '@yjtec/cas-cookie';
import authorized from './Authorized/';
import event from './events';
import CasContext from './CasContext';
import defaultConfig from './config/config.js';
import redirect from './redirect';
import { setParams, getParams } from './params.js';
import CasWrapper from './wrapper';

export function initCas(opt) {

  //初始化config
  setConfig({ ...defaultConfig, ...opt });

  //初始化cookie
  let { cookieOpt } = casConfig.get('cookie.path', '/');
  let type = casConfig.get('type');
  setCookie(
    casConfig.get('type', 'default'),
    casConfig.get('cookie.path', '/'),
    casConfig.get('cookie.domain', null),
  );
}
export function login() {
  let uri = getHost();
  let defaultUri = casConfig.get('type') + '/login';
  uri += '/' + casConfig.get('uri.login', defaultUri) + '?redirect=' + redirect();
  window.location.href = uri;
}
export function logout() {
  let uri = getHost();
  let defaultUri = casConfig.get('type') + '/logout';
  uri += '/' + casConfig.get('uri.logout', defaultUri) + '?redirect=' + redirect();

  event.emit('cas.logout', {
    uri: uri,
  });
}
export function soc(platfrom, callback) {
  if (platfrom === 'wx') {
    platfrom = 'wechat';
  }
  let uri = getHost();
  uri += '/soc/' + casConfig.get('type', 'default') + '/' + platfrom;
  if (callback) {
    //singlegetuser只获取第三方用户信息，返回soc，不直接跳转登录
    uri += '?singlegetuser=true&callback=' + callback;
  }
  window.location.href = uri;
}
export function getHost() {
  return casConfig.get('host', '/');
}

export {
  setConfig,
  casConfig,
  check,
  checkTicket,
  CasContext,
  authorized,
  event,
  cookie,
  checkCookieTicket,
  checkUrlTicket,
  setParams,
  getParams,
  CasWrapper,
};
